package com.woniuxy.amusementparkos.controller.lvd;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.FinancialSummaryChartDataDto;
import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.lvd.CompulsorySettlement;
import com.woniuxy.amusementparkos.entity.lvd.MerchantTransactions;
import com.woniuxy.amusementparkos.entity.lvd.StoreTypeRevenueVisits;
import com.woniuxy.amusementparkos.entity.lvd.SummaryReport;
import com.woniuxy.amusementparkos.service.lvd.FinancialStatisticsService;

@Controller
@RequestMapping("/finStatistics")
public class FinancialStatisticsController {
	@Autowired
	FinancialStatisticsService financialStatisticsService;

	/********************* 汇总统计 *******************/
	/**
	 * 跳转至财务汇总页面
	 * 
	 * @return
	 */
	@RequestMapping("/summaryPage")
	public String toSummaryPage() {
		return "/lvd/financialSummary.html";
	}

	/**
	 * 获取充值总额，消费总额，总收入，总盈利数据
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/total")
	public double[] selectTotal() {
		double[] total = new double[4];
		total[0] = financialStatisticsService.selectTotalRecharge();
		total[1] = financialStatisticsService.selectTotalConsumption();
		total[2] = financialStatisticsService.selectTotalRevenue();
		total[3] = total[2] - financialStatisticsService.selectTotalExpenditure();
		return total;

	}

	/**
	 * 查询汇总表每周周一和周日的日期
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/week")
	public Map<String, String> selectweek() {
		return financialStatisticsService.selectSummaryDate();
	}

	/**
	 * 获取一周之内每天的充值总额、消费总额、总收入和总盈利
	 * 
	 * @param monday
	 * @param sunday
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/weekSummary")
	public FinancialSummaryChartDataDto selectWeekSummary(String monday, String sunday) {
		FinancialSummaryChartDataDto financialSummaryChartDataDto = new FinancialSummaryChartDataDto(
				financialStatisticsService.selectWeekSummaryReports(monday, sunday),
				financialStatisticsService.selectWeekRevenueAndProfitRecord(monday, sunday));
		return financialSummaryChartDataDto;
	}

	/**
	 * 查询汇总报表数据
	 * 
	 * @param page     当前页码
	 * @param pageSize 每页展示数据条数
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/financialSummary")
	public TableDataDto<SummaryReport> financialSummary(@RequestParam("page") int page,
			@RequestParam("limit") int pageSize) {
		// 获取当前页起始数据序号
		page = (page - 1) * pageSize;
		// 获取当前页展示的所有数据
		List<SummaryReport> summaryReports = financialStatisticsService.selectPage(page, pageSize);
		// 获取数据总条数
		long recordCount = financialStatisticsService.selectRecordCount();
		// 封装为Layui表格数据格式
		TableDataDto<SummaryReport> result = new TableDataDto<SummaryReport>(0, "", recordCount, summaryReports);
		return result;
	}

	/**
	 * 修改或者添加汇总表数据
	 * 
	 * @param summaryReport
	 * @return
	 */
	@RequestMapping("/insertOrUpdate")
	public String insertOrUpdateSummaryReport(SummaryReport summaryReport) {
		// id为空则添加
		if (summaryReport.getId() == null) {
			financialStatisticsService.insertSummaryReport(summaryReport);
			// id不为空则修改
		} else {
			financialStatisticsService.updateSummaryReport(summaryReport);
		}
		return "/lvd/financialSummary.html";
	}

	/**
	 * 删除汇总表数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	public String deleteSummaryReport(@RequestBody String[] ids) {
		for (int i = 0; i < ids.length; i++) {
			financialStatisticsService.deleteSummaryReport(ids[i]);
		}
		return "/lvd/financialSummary.html";
	}

	/********************* 商户统计 *******************/
	/**
	 * 跳转至商户统计页面
	 * 
	 * @return
	 */
	@RequestMapping("/merchantPage")
	public String toMerchantPage() {
		return "/lvd/merchantStatistics.html";
	}

	/**
	 * 查询每个商户类型下顾客的消费和访问人次
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/storeTypeRevenueVisits")
	public List<StoreTypeRevenueVisits> storeTypeRevenueVisits() {
		List<StoreTypeRevenueVisits> storeTypeRevenueVisits = financialStatisticsService.selectStoreTypeRevenueVisits();
		return storeTypeRevenueVisits;
	}

	/**
	 * 查询每一周每个商户类型下顾客的消费和访问人次
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/selectWeekStoreTypeRevenueVisits")
	public List<List<StoreTypeRevenueVisits>> selectWeekStoreTypeRevenueVisits() {
		return financialStatisticsService.selectWeekStoreTypeRevenueVisits();
	}

	/**
	 * 查询订单表日期
	 */
	@ResponseBody
	@RequestMapping("/selectOrderDate")
	public List<String> selectOrderDate(HttpServletRequest request) {
		List<String> orderDate = financialStatisticsService.selectOrderDate();
		return orderDate;
	}

	/**
	 * 根据日期范围查询商户交易统计信息
	 */
	@ResponseBody
	@RequestMapping("/selectMerchantTransactions")
	public TableDataDto<MerchantTransactions> selectMerchantTransactions(String start, String end) {
		List<MerchantTransactions> merchantTransactions = financialStatisticsService.selectMerchantTransactions(start,
				end);
		TableDataDto<MerchantTransactions> result = new TableDataDto<MerchantTransactions>(0, "", 10000L,
				merchantTransactions);
		return result;
	}

	@RequestMapping("/forcedSettlement")
	public String tovForcedSettlementPage() {
		return "/lvd/forcedSettlement.html";
	}

	@ResponseBody
	@RequestMapping("/CompulsorySettlement")
	public TableDataDto<CompulsorySettlement> selectCompulsorySettlement(String start, String end, String status,
			String handle, String wriststrpid, int page, @RequestParam("limit") int pageSize) {
		List<CompulsorySettlement> compulsorySettlement = financialStatisticsService
				.selectCompulsorySettlementByCondition(start, end, status, handle, wriststrpid, page, pageSize);
		// 获得查询结果总数据条数
		int size = financialStatisticsService
				.selectCompulsorySettlementByCondition(start, end, status, handle, wriststrpid, 0, 0).size();
		TableDataDto<CompulsorySettlement> result = new TableDataDto<CompulsorySettlement>(0, "", (long) size,
				compulsorySettlement);
		return result;

	}

}
